package com.agricultural.machine.proxy;

import com.agricultural.machine.common.MachineState;
import com.agricultural.machine.common.IMachine;
import com.agricultural.machine.user.UserManager;
import com.agricultural.machine.user.UserRole;

/**
 * 农机代理类 - 代理模式
 */
public class MachineProxy implements IMachineProxy {
    private final IMachine realMachine;
    private final UserManager userManager;

    public MachineProxy(IMachine realMachine) {
        this.realMachine = realMachine;
        this.userManager = UserManager.getInstance();
    }

    @Override
    public IMachine getMachine() {
        return realMachine;
    }

    @Override
    public String getMachineId() {
        return realMachine.getMachineId();
    }

    @Override
    public String getType() {
        return realMachine.getType();
    }

    @Override
    public MachineState getState() {
        return realMachine.getState();
    }

    @Override
    public void setState(MachineState state) {
        if (checkAccess()) {
            realMachine.setState(state);
        } else {
            System.out.println("权限不足：无法修改农机状态");
        }
    }

    @Override
    public void start() {
        if (checkAccess()) {
            realMachine.start();
        } else {
            System.out.println("权限不足：无法启动农机");
        }
    }

    @Override
    public void stop() {
        if (checkAccess()) {
            realMachine.stop();
        } else {
            System.out.println("权限不足：无法停止农机");
        }
    }

    @Override
    public void executeTask(String task) {
        if (checkAccess()) {
            realMachine.executeTask(task);
        } else {
            System.out.println("权限不足：无法执行任务");
        }
    }

    @Override
    public void updatePosition(double x, double y) {
        if (checkAccess()) {
            realMachine.updatePosition(x, y);
        } else {
            System.out.println("权限不足：无法更新位置");
        }
    }

    @Override
    public String getPosition() {
        return realMachine.getPosition();
    }

    @Override
    public double getCurrentX() {
        return realMachine.getCurrentX();
    }

    @Override
    public double getCurrentY() {
        return realMachine.getCurrentY();
    }

    @Override
    public void work() {
        if (checkAccess()) {
            realMachine.work();
        } else {
            System.out.println("权限不足：无法操作农机工作");
        }
    }

    @Override
    public boolean isRunning() {
        return realMachine.isRunning();
    }

    @Override
    public void setEmergencyStop(boolean emergency) {
        if (checkAccess()) {
            realMachine.setEmergencyStop(emergency);
        } else {
            System.out.println("权限不足：无法设置紧急停止状态");
        }
    }

    @Override
    public boolean isEmergencyStopped() {
        return realMachine.isEmergencyStopped();
    }

    @Override
    public String getStatus() {
        return realMachine.getState().getDescription();
    }

    @Override
    public boolean checkAccess() {
        return userManager.hasPermission(UserRole.OPERATOR) || 
               userManager.hasPermission(UserRole.ADMIN);
    }
} 